package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.SystemPermissionEntity;
import vip.specimen.system.repository.SystemPermissionRepository;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * *@Author: 余浪人
 * *@Date：2021/2/22 15:15
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
public class SystemPermissionService {
    @Resource
    private SystemPermissionRepository systemPermissionRepository;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增权限
     *
     * @param permissionEntity
     * @return
     */
    @Transactional
    public SystemPermissionEntity insert(SystemPermissionEntity permissionEntity) {
        return !this.findByPermissionExist(permissionEntity.getPermission()) ? this.systemPermissionRepository.save(permissionEntity) : null;
    }

    /**
     * 根据权限查询是否已存在
     *
     * @param permission 权限
     * @return 是否
     */
    private boolean findByPermissionExist(String permission) {
        SystemPermissionEntity permissionEntity = this.systemPermissionRepository.findByPermission(permission);
        if (null != permissionEntity) return true;
        return false;
    }

    public Page<SystemPermissionEntity> findALlPage() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.systemPermissionRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));
    }
    public List<SystemPermissionEntity> findALl() {
        return this.systemPermissionRepository.findAll(DataBaseUtil.getSort("desc", "createTime"));
    }

    /**
     * 根据uuid 更新状态
     *
     * @param uuid uuid
     * @return 实体
     */
    public SystemPermissionEntity update(UUID uuid) {
        if (this.systemPermissionRepository.existsById(uuid)) {
            SystemPermissionEntity permissionEntity = this.systemPermissionRepository.findById(uuid).get();
            permissionEntity.setEnable(!permissionEntity.isEnable());
            permissionEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.systemPermissionRepository.save(permissionEntity);
        }
        return null;
    }

    /**
     * 根据uuid 列表查询 权限
     *
     * @param uuids uuid =》 list
     * @return 权限列表
     */
    public List<SystemPermissionEntity> findAllById(Set<UUID> uuids) {
        return this.systemPermissionRepository.findAllById(uuids);
    }

    /**
     * 批量添加权限
     *
     * @param mapList 权限列表
     * @return 权限列表(实体)
     */
    @Transactional
    public List<SystemPermissionEntity> insert(Set<Map<String, Object>> mapList) {
        List<SystemPermissionEntity> systemPermissionEntityList = this.systemPermissionRepository.findByResourceType("button");
        if (systemPermissionEntityList.size() == mapList.size()) return systemPermissionEntityList;
        List<SystemPermissionEntity> list = new ArrayList<>();
        mapList.forEach(map -> {
            String parenIdStr = (String) map.get("parenId");
            SystemPermissionEntity permissionEntity = (SystemPermissionEntity) map.get("permission");
            SystemPermissionEntity paren = this.systemPermissionRepository.findByPermissionName(parenIdStr);
            paren = (null == paren) ? this.systemPermissionRepository.save(new SystemPermissionEntity(parenIdStr, "menu", "-", "*", null)) : paren;
            if (!this.findByPermissionExist(permissionEntity.getPermission())) {
                permissionEntity.setParent(paren);
                permissionEntity.setEnable(true);
                list.add(permissionEntity);
            }
        });
        return this.systemPermissionRepository.saveAll(list);
    }
}
